package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.EYSystem.dao.NursingProjectDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.NursingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursingProject")
public class NursingProjectController {

    // ✅ 工具方法：判断是否为合法字符串（排除 null、空串、"null"）
    private boolean isValid(String value) {
        return value != null && !value.trim().isEmpty() && !"null".equalsIgnoreCase(value.trim());
    }
    @Autowired
    private NursingProjectService nursingProjectService;
    @Autowired
    private NursingProjectDAO nursingProjectDAO;

    //管理员端：护理管理护理项目表格显示
    //可查询条件包括projectId name status
    @GetMapping("/pageNursingProject") // GET请求，路径为 /nursingProject/pageNursingProject
    public PageResponseBean<List<NursingProject>> pageNursingProject(Long cur, Long number, NursingProject nursingProject) {
        return nursingProjectService.pageNursingProject(cur, number, nursingProject);
    }

    //管理员端：护理管理护理项目弹窗添加护理项目
    @PostMapping("/addNursingProject")
    public ResponseBean<Integer> addNursingProject(NursingProject nursingProject) {
        ResponseBean<Integer> response = new ResponseBean<>();
        try {
            if(nursingProjectDAO.insert(nursingProject) > 0){
                response.setStatus(200);
                response.setMsg("护理项目添加成功");
                return response;
            }else {
                response.setStatus(400);
                response.setMsg("护理项目添加失败，可能是数据不完整或重复");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("护理项目添加失败: " + e.getMessage());
        }
        return response;
    }

    //管理员端：护理管理护理项目表格删除护理项目按钮
    @PostMapping("/deleteNursingProjectById")
    public ResponseBean<Integer> deleteNursingProjectById(NursingProject nursingProject) {
        Integer result;
        ResponseBean<Integer> rb = null;
        try {
            result = nursingProjectService.deleteStatusById(nursingProject);
            rb =new ResponseBean<>(result);

        } catch (Exception e) {
            e.printStackTrace();
            rb =new ResponseBean<>(500,"删除护理项目错误");
        }
        return rb;
    }

    //管理员端：护理管理护理项目表格弹窗修改护理项目
    @PostMapping("/updateNursingProject")
    public ResponseBean<Integer> updateNursingProject(NursingProject nursingProject) {
        Integer result;
        ResponseBean<Integer> rb = null;
        UpdateWrapper<NursingProject> uw = new UpdateWrapper<>();
        uw.eq("project_id", nursingProject.getProjectId());
        try{
            result = nursingProjectDAO.update(nursingProject, uw);
            rb = new ResponseBean<>(result);
        }catch (Exception e){
            e.printStackTrace();
            rb =new ResponseBean<>(500,"修改护理项目错误");
        }
        return rb;
    }

}
